import { ESCesiumViewer, PlaceEditing } from "@czmSrc/index";
import { EngineObject, ESSceneObject } from "earthsdk3"
import { react } from 'xbsj-base';
import { Editing } from "../base/Editing";
import { MultiPointsAppendEditing } from "../base/MultiPointsEditing/MultiPointsAppendEditing";

export abstract class BaseEditingWrapper extends EngineObject {
    private _supportEditing = this.disposeVar(react<boolean>(true));
    get supportEditing() { return this._supportEditing.value; }
    set supportEditing(value: boolean) { this._supportEditing.value = value; }
    get supportEditingChanged() { return this._supportEditing.changed; }

    private _currentEditing = this.disposeVar(react<Editing | undefined>(undefined));
    get currentEditing() { return this._currentEditing.value; }
    set currentEditing(value: Editing | undefined) { this._currentEditing.value = value; }
    get currentEditingChanged() { return this._currentEditing.changed; }
    constructor(sceneObject: ESSceneObject, czmViewer: ESCesiumViewer, isEditing?: boolean) {
        super(sceneObject, czmViewer, isEditing ?? true);
        this.ad(this.supportEditingChanged.don(() => {
            this.destroy(); //销毁
        }))
        this.ad(this.toDestroyEvent.don(() => {
            if (!this.currentEditing) return;
            if (this.currentEditing instanceof MultiPointsAppendEditing
                && this.currentEditing.currentProcess?.isRunning
            ) {
                this.currentEditing.currentProcess.cancel();
                //@ts-ignore
                if (sceneObject?.position) {
                    if (this.currentEditing.positions.length > 1) return;
                    //@ts-ignore
                    sceneObject.position = this.currentEditing.positions[0] ?? [0, 0, 0];
                    //@ts-ignore
                    sceneObject?.rotation && (sceneObject.rotation = [0, 0, 0]);
                    //@ts-ignore
                    sceneObject?.far && (sceneObject.far = 1);
                    //@ts-ignore
                    sceneObject?.radius && (sceneObject.radius = 1);
                    //@ts-ignore
                    sceneObject?.distance && (sceneObject.distance = 1);
                } else {
                    //@ts-ignore
                    sceneObject.points = this.currentEditing.positions;
                }
            }
            if (this.currentEditing instanceof PlaceEditing) {
                //@ts-ignore
                this.currentEditing.enabled && (sceneObject.position = [0, 0, 0]);
            }
        }))
    }
}